package com.sandking.io;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/**
 * @UserName : SandKing
 * @DataTime : 2012-9-18 上午10:44:20
 * @Description ：Please describe this document
 */
public class SK_File {
	/**
	 * 递归删除文件加下的某个文件
	 * 
	 * @param file
	 * @param filename
	 */
	public static void deleteFileByFileName(File file, String filename) {
		File[] fs = file.listFiles();
		for (File f : fs) {
			if (f.isDirectory()) {
				deleteFileByFileName(f, filename);
			} else {
				if (filename.equals(f.getName()))
					f.delete();
			}
		}
	}

	// 递归找到所有的这个文件夹下的这种文件
	public static String deleteFileByFileName1(File file, String filename) {
		File[] fs = file.listFiles();
		for (File f : fs) {
			if (f.isDirectory()) {
				deleteFileByFileName(f, filename);
			} else {
				if (filename.equals(f.getName())) {
					f.delete();
				}
			}
		}
		return "Delete all files";
	}

	public static int deleteFileByFileName(File file, String filename, int i) {
		File[] fs = file.listFiles();
		for (File f : fs) {
			if (f.isDirectory()) {
				i = deleteFileByFileName(f, filename, i);
			} else {
				if (filename.equals(f.getName())) {
					f.delete();
					i++;
				}
			}
		}
		return i;
	}

	public static int deleteFileByFileName(String dir, String filename) {
		int i = 0;
		File file = new File(dir);
		return deleteFileByFileName(file, filename, i);
	}

	/**
	 * 3.读取字符,打印文本
	 */
	// FileReader,InputStreamReader默认的字符编码
	public static void printf(String file) {
		try {
			InputStreamReader in = new FileReader(file);
			int n, time = 1;
			while ((n = in.read()) != -1) {
				char c = (char) n;// 返回的是char的16进制
				time++;
				System.out.print(c);
			}
			System.out.println("总共字数:" + time);
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 3.读取字符,打印文本
	 */
	// FileInputStream字节读入,InputStreamReader过滤器,BufferedInputStream缓冲,指定字符编码读取
	public static void printf(File file, String encoding) {
		try {
			InputStreamReader in = new InputStreamReader(
					new BufferedInputStream(new FileInputStream(file)),
					encoding);// 加入了缓冲
			char[] ch = new char[1024 * 4];// 自定义缓冲
			int n, time = 1;
			while ((n = in.read(ch)) != -1) {
				time++;
				in.read(ch, 0, n);
				for (char c : ch) {
					System.out.print(c);
				}
			}
			System.out.println("总共字数:" + time);
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 3.读取字符,打印文本
	 */
	// BufferedReader,读取打印文本
	public static void printf(File file) {
		DataInputStream dis;
		try {
			dis = new DataInputStream(new BufferedInputStream(
					new FileInputStream(file)));
			BufferedReader in = new BufferedReader(new InputStreamReader(dis));
			int i, time = 0;
			while ((i = in.read()) != -1) {
				System.out.print((char) i);
				time++;
			}
			// 下面的可以读取,但是不靠谱.
			// char c;
			// try {
			// while ((int)(c = dis.readChar()) != -1) {
			// System.out.print(c);
			// }
			// } catch (EOFException e) {
			// System.out.println("读取完毕了");
			// }
			System.out.println("总字数:" + time);
			dis.close();
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 3.按指定编码读取文本,返回一个字符集合.
	 */
	// 指定编码读文本,读的最小单位是字符,2byte.
	public static ArrayList<Character> readByEncoding(File file, String encoding) {
		// 将文本保存到集合中
		ArrayList<Character> list = new ArrayList<Character>();
		try {
			InputStreamReader isr = new InputStreamReader(
					new BufferedInputStream(new FileInputStream(file)),

					encoding);
			int i; /* time = 1 */
			while ((i = isr.read()) != -1) {
				list.add((char) i);
				// System.out.print((char) i + " " + i + " "
				// + Integer.toHexString(i));
				// System.out.println(time++ % 10 == 0 ? "\n" : " ");
			}
			isr.close();
			return list;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
}
